import { _decorator, Component, Node, resources, ImageAsset, Prefab, instantiate, Sprite, SpriteFrame } from 'cc';
import { Global } from '../Global';
import { HeroManage } from '../Hero/HeroManage';
import { EntityManager } from './EntityManager';
const { ccclass, property } = _decorator;

@ccclass('FloorManager')
export class FloorManager extends Component {
    @property(Prefab)
    wall: Prefab = null

    @property(Node)
    wallBack: Node = null

    @property({ type: EntityManager })
    entityManager: EntityManager = null

    @property({ type: HeroManage })
    heroManage: HeroManage = null

    private _currentFloor: number = 0

    start() {
        Global.fm = this
        this.genDefaultWalls()
    }

    update(deltaTime: number) {

    }

    loadFloor(floor: number, heroState?: string) {
        this.entityManager.loadEntity(floor)
        const heroPos = this.entityManager.loadHero(floor, heroState)
        this.heroManage.resetPos(heroPos[0], heroPos[1], floor)
        this._currentFloor = floor
    }

    loadNextFloor(heroState: string) {
        if (this._currentFloor >= 25) {
            return
        }
        console.log('go upstairs')
        Global.hc.removeListener()
        Global.sm.show(this._currentFloor + 1)
        this.loadFloor(this._currentFloor + 1, heroState)
        Global.hc.addListener()
    }

    loadPreFloor(heroState: string) {
        if (this._currentFloor <= 0) {
            return
        }
        console.log('go downstairs')
        Global.hc.removeListener()
        Global.sm.show(this._currentFloor - 1)
        this.loadFloor(this._currentFloor - 1, heroState)
        Global.hc.addListener()
    }

    getFloor(): number {
        return this._currentFloor
    }

    private genDefaultWalls() {
        for (let i = 0; i < 13; i++) {
            for (let j = 0; j < 13; j++) {
                if (i === 0 || i === 12 || j === 0 || j === 12) {
                    let wall = instantiate(this.wall);
                    wall.setPosition(14 + 28 * i, 14 + 28 * j)
                    this.wallBack.addChild(wall)
                }
            }
        }
    }
}

